$(function () {
    $("#jqGrid").jqGrid({
        url: '../socialsecuritytransaction/list',
        datatype: "json",
		//postData: {"confirmStatus":"0,1,2","processStatus":"1,2"},
        colModel: [
            { label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden:true},
            { label: '单位id', name: 'unitId', index: 'unit_id' ,hidden:true},
            { label: '单位名称', name: 'settlement_name', index: 'settlement_name'},
            { label: '员工姓名', name: 'name', index: 'name' },
            { label: '员工id', name: 'employ_id', index: 'employ_id', hidden:true },
            { label: '人员派遣id', name: 'person_dispatch_id', index: 'person_dispatch_id' ,hidden:true},
            { label: '申报类型(0申保，1退保)', name: 'declare_type', index: 'declare_type' ,formatter:getDictInfo},
            { label: '保险类型', name: 'insurance_type', index: 'insurance_type' ,formatter:getDictInfo},
            { label: '申报时间', name: 'declare_date', index: 'declare_date' },
            { label: '申报人', name: 'declare_person', index: 'declare_person' },
            { label: '备注', name: 'remark', index: 'remark' },
            { label: '用工形式', name: 'employment_form', index: 'employment_form' },
            { label: '异动原因', name: 'transaction_reason', index: 'transaction_reason' },
            { label: '初审人', name: 'trial_judge', index: 'trial_judge' },
            { label: '初审日期', name: 'Trial_date', index: 'Trial_date' },
            { label: '终审人', name: 'chief_judge', index: 'chief_judge' },
            { label: '终审日期', name: 'chief_date', index: 'chief_date' },
            { label: '社保申报状态(0，待申报,1,申报中，2，已申报，3，退回,4，不申报,5,重新申报，6，临时退保)', name: 'process_status', index: 'process_status', formatter:getprocessStatus,unformat:ungetprocessStatus },
            { label: '审核状态(0，未审核，1,初审，2,终审，3，反审，4，退回)', name: 'confirm_status', index: 'confirm_status',  formatter:getconfirmStatus,unformat:ungetconfirmStatus},
            { label: '退回说明', name: 'return_explain', index: 'return_explain' }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        //字段太多自动加载横轴滚动条
        shrinkToFit: false,
        autoScroll: false,
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	//$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
    var r = utils.getDictList("declare,socialSecurity");
    vm.declare=r.declare;
    vm.socialSecurity=r.socialSecurity;
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		socialSecurityTransaction: {},
        declare:{},
        socialSecurity:{},
        q:{
            dateRange:null,
            declareStartDateStr:null,
            declareEndDateStr:null,
            declare:null,
            socialSecurity:null,
            confirmStatus:null,
            declarePerson:null
        }
	},
	methods: {
		query: function () {
			vm.reload();
		},
        firstTrial: function(){
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            var params = new Object();
            params.type = "confirm";
            params.operation = "firstTrial";
            params.ids = ids;
            $.ajax({
                type: "POST",
                url: "../socialsecuritytransaction/updateStatus",
                contentType: "application/json",
                data: JSON.stringify(params),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
		},
        recheck: function(){
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            var params = new Object();
            params.type = "confirm";
            params.operation = "recheck";
            params.ids = ids;
            $.ajax({
                type: "POST",
                url: "../socialsecuritytransaction/updateStatus",
                contentType: "application/json",
                data: JSON.stringify(params),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        backTrial: function(){
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            var params = new Object();
            params.type = "confirm";
            params.operation = "backTrial";
            params.ids = ids;
            $.ajax({
                type: "POST",
                url: "../socialsecuritytransaction/updateStatus",
                contentType: "application/json",
                data: JSON.stringify(params),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        failedCheck: function(){
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            var params = new Object();
            params.type = "confirm";
            params.operation = "failedCheck";
            params.ids = ids;
            $.ajax({
                type: "POST",
                url: "../socialsecuritytransaction/updateStatus",
                contentType: "application/json",
                data: JSON.stringify(params),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.socialSecurityTransaction = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.socialSecurityTransaction.id == null ? "../socialsecuritytransaction/save" : "../socialsecuritytransaction/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.socialSecurityTransaction),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../socialsecuritytransaction/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../socialsecuritytransaction/info/"+id, function(r){
                vm.socialSecurityTransaction = r.socialSecurityTransaction;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    declareStartDateStr:vm.q.declareStartDateStr,
                    declareeEndDateStr:vm.q.declareeEndDateStr,
                    declare:vm.q.declare,
                    socialSecurity:vm.q.socialSecurity,
                    confirm_status:vm.q.confirmStatus,
                    declarePerson:vm.q.declarePerson
                },
                page:page
            }).trigger("reloadGrid");
		},
        dateChange:function(val){
            vm.q.declareStartDateStr=val.split(" - ")[0];
            vm.q.declareeEndDateStr=val.split(" - ")[1];
        }
	}
});

/*function getdeclareType(cellvalue, options, rowObject){
    var html="";
    if(cellvalue=="0"){
        html="申保";
    }else if(cellvalue=="1"){
        html="退保";
    }
    return html;
}
function ungetdeclareType(cellvalue, options, rowObject){
    var html="";
    if(cellvalue=="申保"){
        html="0";
    }else if(cellvalue=="退保"){
        html="1";
    }
    return html;
}*/
function getprocessStatus(cellvalue, options, rowObject){
    var html="";
    if(cellvalue=="0"){
        html="待申报";
    }else if(cellvalue=="1"){
        html="申报中";
    }else if(cellvalue=="2"){
        html="已申报";
    }else if(cellvalue=="3"){
        html="退回";
    }else if(cellvalue=="4"){
        html="不申报";
    }else if(cellvalue=="5"){
        html="重新申报";
    }else if(cellvalue=="6"){
        html="临时退保";
    }
    return html;
}
function ungetprocessStatus(cellvalue, options, rowObject){
    var html="";
    if(cellvalue=="待申报"){
        html="0";
    }else if(cellvalue=="申报中"){
        html="1";
    }else if(cellvalue=="已申报"){
        html="2";
    }else if(cellvalue=="退回"){
        html="3";
    }else if(cellvalue=="不申报"){
        html="4";
    }else if(cellvalue=="重新申报"){
        html="5";
    }else if(cellvalue=="临时退保"){
        html="6";
    }
    return html;
}

function getconfirmStatus(cellvalue, options, rowObject){
    var html="";
    if(cellvalue=="0"){
        html="未审核";
    }else if(cellvalue=="1"){
        html="初审";
    }else if(cellvalue=="2"){
        html="终审";
    }else if(cellvalue=="3"){
        html="反审";
    }else if(cellvalue=="4"){
        html="退回";
    }
    return html;
}
function ungetconfirmStatus(cellvalue, options, rowObject){
    var html="";
    if(cellvalue=="未审核"){
        html="0";
    }else if(cellvalue=="初审"){
        html="1";
    }else if(cellvalue=="终审"){
        html="2";
    }else if(cellvalue=="反审"){
        html="3";
    }else if(cellvalue=="退回"){
        html="4";
    }
    return html;
}